<?php

require_once 'ini.php';
use admin\helper\qiniu ;
//当前域名
$site = new \admin\helper\site($register);

/**
 * 首页保存
 */
if (isset($_POST['act']) && $_POST['act'] == 'save') {

    $domain = null;
    if (isset($_POST['domain'])) {
        $domain = $_POST['domain'];
    }

    //判断是否有可编译权限
    $allDomain = $site->getAllDomain();
    $data = $_POST;
    unset($data['update']);
    unset($data['act']);
    if($data['logo'])
    {
        $data['logo'] = qiniu::changImgDomain($data['logo']);
    }
    if (in_array($domain, $allDomain)) {
        if ($_POST['update']) {
            //更新
            $filter['domain'] = $domain;
            $site->updateSite($filter, $data);
        } else {
            //插入
            $site->saveSite($data);
        }
    }
    if( isset($_GET['json']) && $_GET['json']==1){
        $data['code'] = 1;
        echo json_encode($data);
    }else{
        // header('location:site.php?act=search&domain='.$domain);
    }
}
/**
 *  删除主页
 */
else if (isset($_POST['act']) && $_POST['act'] == 'delete') {
    $domain = $_POST['domain'];
    $data['is_del'] = $_POST['is_del'];
    $ret = $site->deleteSite($domain, $data);
    echo json_encode($ret);
}
/**
 * 首页编辑
 */
else if (isset($_GET['act']) && $_GET['act'] == 'edit') {
    $data = $site->getSiteInfo($_GET['domain']);
    if( isset($_GET['json']) && $_GET['json']==1 ){
        echo json_encode($data);
    }else{
        $register->get('view')->display('/site.twig', $data);
    }
}
/**
 *  根据域名搜索
 */
else if(isset($_GET['act']) && $_GET['act'] == 'search') {
    $domain = $_GET['domain'];
    $data = [];
    if (!empty($domain)) {
        $filter['domain'] = $domain;
        $data = $site->searchDomain($filter);
    }
    //构建菜单
    $keyword['domain'] = $domain;
    $data['filter'] = $keyword;
    if( isset($_GET['json']) && $_GET['json']=='1'){
        echo json_encode($data);
    }else{
        $register->get('view')->display('/site_list.twig', $data);
    }
}
/**
 * 默认主页设置列表
 */
else {
    //查询当前可编辑域名
//    $data = $site->getListDomain();
    $data =  $_SESSION;

    $register->get('view')->display('/site_list.twig', $data);
}